package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Host;
import com.j48.entrancegurad.service.IHostService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HostController {
    @Autowired
    private IHostService hostservice;

    /**
     * 查询全部业主
     * @param request
     * @return 返回页面
     */
    @RequestMapping("/HostList")
    @ResponseBody
    public ModelAndView HostList(HttpServletRequest request){
        ModelAndView modelAndView=null;
        List<Host> hostList=new ArrayList<Host>();
        hostList=hostservice.selectAll();
        request.getSession().setAttribute("host",hostList);
        modelAndView=new ModelAndView("jsp/host");
        return  modelAndView;
    }
    /**
     * 查询业主信息
     * @param host_name 传入的业主名
     * @param request
     * @return 返回跳转的页面
     */
    @RequestMapping("/selectHost")
    @ResponseBody
    public ModelAndView selectHost(String host_name, HttpServletRequest request){
        if(null==host_name||"".equals(host_name)){
            return HostList(request);
        }
        ModelAndView model=null;
        Host ht = new Host();
        ht.setHost_name(host_name);
        List<Host> lists = new ArrayList<Host>();
        lists=hostservice.selectByName(ht);
        model=new ModelAndView("jsp/host");
        model.addObject("host",lists);
        return model;
    }

    /**
     * 添加业主信息
     * @param host 业主对象
     * @return 跳转的页面
     */
    @RequestMapping("/hostAddController")
    @ResponseBody
    public ModelAndView hostAddController(Host host){
        ModelAndView model=null;
        Date date = new Date();
        Timestamp admin_time=new Timestamp(date.getTime());
        Host ht=new Host(host.getHost_building(),host.getHost_unit(),host.getHost_housenumber(),host.getHost_name(),host.getHost_phone(),host.getHost_number(),host.getHost_create_admin_id(),admin_time);
        Host host1 = hostservice.selectByHouse(new Host(ht.getHost_building(),ht.getHost_unit(),ht.getHost_housenumber()));
//        System.out.println(host1.getHost_name());
        if (host1==null){
            if (hostservice.addHost(ht)){
                model=new ModelAndView("jsp/success");
            }else {
                model=new ModelAndView("jsp/failure");
            }
        }else {
            model=new ModelAndView("jsp/failure");
        }
        return model;
    }

    /**
     * 修改业主前查询业主信息
     * @param request
     * @return
     */
    @RequestMapping("/updateHostPage")
    @ResponseBody
    public String updateHostPage(HttpServletRequest request){
        int host_id=Integer.parseInt(request.getParameter("host_id"));
        Host host=new Host();
        host.setHost_id(host_id);
        Host ht=hostservice.selectById(host);
        request.getSession().setAttribute("selecthost",hostservice.selectById(host));
        return "true";
    }

    /**
     * 跳转到修改业主页面
     * @param request
     * @return
     */
    @RequestMapping("/updateHost")
    @ResponseBody
    public ModelAndView updateHost(HttpServletRequest request){
        return new ModelAndView("jsp/hostUpdate");
    }

    /**
     * 修改业主信息
     * @param host 修改后的业主
     * @return
     */
    @RequestMapping("/updateHostController")
    @ResponseBody
    public ModelAndView updateAdminController(Host host){
        ModelAndView model=null;
        int admin_id=host.getHost_id();

        if (hostservice.updateHost(host)){
            model=new ModelAndView("jsp/hostsuccess");
        }else {
            model=new ModelAndView("jsp/hostfailure");
        }
        return model;
    }
}
